// pages/movies/movie-detail/movie-detail.js
// 引入公用组件
var util = require("../../../utils/utils.js");
// 获取全局APP
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movie: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var movieId = options.id;
    var url = app.globalData.doubanBase + "/v2/movie/subject/" + movieId;
    // // 调用请求函数
    util.http(url, this.processDoubanData);
  },
  processDoubanData: function(data) {
    console.log(data);
    // 对data的判空
    if (!data) {
      return;
    }
    // 后台返回的数据
    var director = {
      avatar: "",
      name: "",
      id: ""
    }
    if (data.directors[0] != null) {
      if (data.directors[0].avatars != null) {
        director.avatar = data.directors[0].avatars.large;
      }
      director.name = data.directors[0].name;
      director.id = data.directors[0].id;
    }
    var movie = {
      // 如果data.images存在,取值data.images.large,否则取空字符串
      movieImg: data.images ? data.images.large : "",
      country: data.countries[0],
      title: data.title,
      originalTitle: data.original_title,
      wishCount: data.wish_count,
      commentCount: data.comments_count,
      year: data.year,
      // 用顿号连接数组元素
      generes: data.genres.join("、"),
      stars: util.convertToStarsArray(data.rating.stars),
      score: data.rating.average,
      director: director,
      casts: util.convertToCastString(data.casts),
      castsInfo: util.convertToCastInfos(data.casts),
      summary: data.summary
    }
    // console.log(movie);
    this.setData({
      movie: movie
    })
  },
  // 查看图片
  viewMoviePostImg: function(e) {
    var src = e.currentTarget.dataset.src;
    console.log(src);
    wx.previewImage({
      urls: [src], //需要预览的图片http链接列表，
      current: src //当前显示图片的http链接
    })
  }

})